/*******************************************************************/
/* @ All Rights Reserved Copyright (C) 2014 broada,Co.,Ltd.        */
/*                                                                 */
/*  NAME      = t_wtc_packet_unfragment.c                         */
/*  NOTE      =                                                    */
/*  DATE      = 2016/02/24 by songbb                               */
/*******************************************************************/

#include <CUnit/Basic.h>
#include <stdlib.h>

#include "frcomm.h"
#include "frmem.h"

#include "../../../src/module/tuxedowtc/config.h"
#include "../test_wtc_comm.h"
#include <tuxedowtc/wtc_core.h>
#include <tuxedowtc/wtc_header.h>
#include <tuxedowtc/wtc_comm.h>
#include <tuxedowtc/wtc_packet.h>
#include <frct_comm.h>
#include <apmtest_comm.h>

extern void __wtc_destroy_session(L7session_wtc **pp_st_session);
extern void __wtc_destroy_packet(L7_wtc_packet **pp_st_wtc_packet);

static int __suite_init(void)
{
    return 0;
}

static int __suite_cleanup(void)
{
    return 0;
}

static void __case1(void)
{
    L7session_wtc *p_st_wtc_sess = NULL;
    p_st_wtc_sess = (L7session_wtc *) frmalloc(sizeof(L7session_wtc));
    CU_ASSERT_PTR_NOT_NULL(p_st_wtc_sess);
    memset(p_st_wtc_sess, 0x00, sizeof(L7session_wtc));

    char *p_bin = (char *) "739038420000000e00000001ffffffff0000000000000000000000010000021c0000005700000054000000430000004c0000006f00000063000000610000006c000000410000005000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000004800000014000000010001001b";
    char arr_payload[65536] = {'\0'};

    CU_ASSERT_EQUAL(apmtest_hex_string_to_bin(p_bin, arr_payload, sizeof(arr_payload)), FR_SUCC);

    unsigned int ui_payload_len = strlen(p_bin) / 2;
    Apm_packet *p_st_packet = NULL;
    struct timeval st_timestamp;
    st_timestamp.tv_sec = 1454549354L;
    st_timestamp.tv_usec = 123456L;
    CU_ASSERT_EQUAL(apmtest_create_apm_packet(&p_st_packet, arr_payload, ui_payload_len, TEST_WTC_SRC_IP, TEST_WTC_DST_IP, TEST_WTC_SRC_PORT, TEST_WTC_DST_PORT, st_timestamp), FR_SUCC);
    p_st_packet->i_direction = FR_DIRECT_CLIENT2SRV;

    L7_wtc_packet *p_st_wtc_packet = NULL;
    p_st_wtc_packet = (L7_wtc_packet *) frmalloc(sizeof(L7_wtc_packet));
    CU_ASSERT_PTR_NOT_NULL(p_st_wtc_packet);
    memset(p_st_wtc_packet, 0x00, sizeof(L7_wtc_packet));

    CU_ASSERT_EQUAL(wtc_init_packet(p_st_wtc_packet), FR_SUCC);

    p_st_wtc_packet->fragment_state = APM_WTC_FRAGMENT_NEED_NEXT_REQ_PACKET;

    CU_ASSERT_EQUAL(wtc_packet_unfragment(p_st_wtc_sess, p_st_packet, p_st_wtc_packet), APM_WTC_CHECK_FRAGMENT_SUCCESS);
    CU_ASSERT_EQUAL(p_st_wtc_packet->st_timestamp_last.tv_sec, 1454549354L);

    __wtc_destroy_packet(&p_st_wtc_packet);
    CU_ASSERT_PTR_NULL(p_st_wtc_packet);

    apmtest_destroy_apm_packet(&p_st_packet);
    CU_ASSERT_PTR_NULL(p_st_packet);

    __wtc_destroy_session(&p_st_wtc_sess);
    CU_ASSERT_PTR_NULL(p_st_wtc_sess);
}

static void __case2(void)
{
    L7session_wtc *p_st_wtc_sess = NULL;
    p_st_wtc_sess = (L7session_wtc *) frmalloc(sizeof(L7session_wtc));
    CU_ASSERT_PTR_NOT_NULL(p_st_wtc_sess);
    memset(p_st_wtc_sess, 0x00, sizeof(L7session_wtc));

    char *p_bin = (char *) "739038420000000e00000001ffffffff0000000000000000000000010000021c0000005700000054000000430000004c0000006f00000063000000610000006c000000410000005000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000004800000014000000010001001b";
    char arr_payload[65536] = {'\0'};

    CU_ASSERT_EQUAL(apmtest_hex_string_to_bin(p_bin, arr_payload, sizeof(arr_payload)), FR_SUCC);

    unsigned int ui_payload_len = strlen(p_bin) / 2;
    Apm_packet *p_st_packet = NULL;
    struct timeval st_timestamp;
    st_timestamp.tv_sec = 1454549354L;
    st_timestamp.tv_usec = 123456L;
    CU_ASSERT_EQUAL(apmtest_create_apm_packet(&p_st_packet, arr_payload, ui_payload_len, TEST_WTC_SRC_IP, TEST_WTC_DST_IP, TEST_WTC_SRC_PORT, TEST_WTC_DST_PORT, st_timestamp), FR_SUCC);
    p_st_packet->i_direction = FR_DIRECT_CLIENT2SRV;

    L7_wtc_packet *p_st_wtc_packet = NULL;
    p_st_wtc_packet = (L7_wtc_packet *) frmalloc(sizeof(L7_wtc_packet));
    CU_ASSERT_PTR_NOT_NULL(p_st_wtc_packet);
    memset(p_st_wtc_packet, 0x00, sizeof(L7_wtc_packet));

    CU_ASSERT_EQUAL(wtc_init_packet(p_st_wtc_packet), FR_SUCC);

    p_st_wtc_packet->fragment_state = APM_WTC_FRAGMENT_NEED_NEXT_RESP_PACKET;

    CU_ASSERT_EQUAL(wtc_packet_unfragment(p_st_wtc_sess, p_st_packet, p_st_wtc_packet), APM_WTC_CHECK_FRAGMENT_SUCCESS);
    CU_ASSERT_EQUAL(p_st_wtc_packet->st_timestamp_last.tv_sec, 1454549354L);

    __wtc_destroy_packet(&p_st_wtc_packet);
    CU_ASSERT_PTR_NULL(p_st_wtc_packet);

    apmtest_destroy_apm_packet(&p_st_packet);
    CU_ASSERT_PTR_NULL(p_st_packet);

    __wtc_destroy_session(&p_st_wtc_sess);
    CU_ASSERT_PTR_NULL(p_st_wtc_sess);
}

static void __case3(void)
{
    L7session_wtc *p_st_wtc_sess = NULL;
    p_st_wtc_sess = (L7session_wtc *) frmalloc(sizeof(L7session_wtc));
    CU_ASSERT_PTR_NOT_NULL(p_st_wtc_sess);
    memset(p_st_wtc_sess, 0x00, sizeof(L7session_wtc));

    char *p_bin = (char *) "739038420000000f00000001ffffffff0000000000000000000000010000022000000073000000690000006d000000700000006100000070000000700000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000007900000014000000010000001b";
    char arr_payload[65536] = {'\0'};

    CU_ASSERT_EQUAL(apmtest_hex_string_to_bin(p_bin, arr_payload, sizeof(arr_payload)), FR_SUCC);

    unsigned int ui_payload_len = strlen(p_bin) / 2;
    Apm_packet *p_st_packet = NULL;
    struct timeval st_timestamp;
    st_timestamp.tv_sec = 1454549354L;
    st_timestamp.tv_usec = 123456L;
    CU_ASSERT_EQUAL(apmtest_create_apm_packet(&p_st_packet, arr_payload, ui_payload_len, TEST_WTC_SRC_IP, TEST_WTC_DST_IP, TEST_WTC_SRC_PORT, TEST_WTC_DST_PORT, st_timestamp), FR_SUCC);
    p_st_packet->i_direction = FR_DIRECT_SRV2CLIENT;

    L7_wtc_packet *p_st_wtc_packet = NULL;
    p_st_wtc_packet = (L7_wtc_packet *) frmalloc(sizeof(L7_wtc_packet));
    CU_ASSERT_PTR_NOT_NULL(p_st_wtc_packet);
    memset(p_st_wtc_packet, 0x00, sizeof(L7_wtc_packet));

    CU_ASSERT_EQUAL(wtc_init_packet(p_st_wtc_packet), FR_SUCC);

    p_st_wtc_packet->fragment_state = APM_WTC_FRAGMENT_NEED_NEXT_RESP_PACKET;

    CU_ASSERT_EQUAL(wtc_packet_unfragment(p_st_wtc_sess, p_st_packet, p_st_wtc_packet), APM_WTC_CHECK_FRAGMENT_SUCCESS);
    CU_ASSERT_EQUAL(p_st_wtc_packet->st_timestamp_last.tv_sec, 1454549354L);

    __wtc_destroy_packet(&p_st_wtc_packet);
    CU_ASSERT_PTR_NULL(p_st_wtc_packet);

    apmtest_destroy_apm_packet(&p_st_packet);
    CU_ASSERT_PTR_NULL(p_st_packet);

    __wtc_destroy_session(&p_st_wtc_sess);
    CU_ASSERT_PTR_NULL(p_st_wtc_sess);
}

static void __case4(void)
{
    L7session_wtc *p_st_wtc_sess = NULL;
    p_st_wtc_sess = (L7session_wtc *) frmalloc(sizeof(L7session_wtc));
    CU_ASSERT_PTR_NOT_NULL(p_st_wtc_sess);
    memset(p_st_wtc_sess, 0x00, sizeof(L7session_wtc));

    char *p_bin = (char *) "739038420000000e00000001ffffffff0000000000000000000000010000021c0000005700000054000000430000004c0000006f00000063000000610000006c00000041000000500000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000480000001400000001000100";
    char arr_payload[65536] = {'\0'};

    CU_ASSERT_EQUAL(apmtest_hex_string_to_bin(p_bin, arr_payload, sizeof(arr_payload)), FR_SUCC);

    unsigned int ui_payload_len = strlen(p_bin) / 2;
    Apm_packet *p_st_packet = NULL;
    struct timeval st_timestamp;
    st_timestamp.tv_sec = 1454549354L;
    st_timestamp.tv_usec = 123456L;
    CU_ASSERT_EQUAL(apmtest_create_apm_packet(&p_st_packet, arr_payload, ui_payload_len, TEST_WTC_SRC_IP, TEST_WTC_DST_IP, TEST_WTC_SRC_PORT, TEST_WTC_DST_PORT, st_timestamp), FR_SUCC);
    p_st_packet->i_direction = FR_DIRECT_CLIENT2SRV;

    L7_wtc_packet *p_st_wtc_packet = NULL;
    p_st_wtc_packet = (L7_wtc_packet *) frmalloc(sizeof(L7_wtc_packet));
    CU_ASSERT_PTR_NOT_NULL(p_st_wtc_packet);
    memset(p_st_wtc_packet, 0x00, sizeof(L7_wtc_packet));

    CU_ASSERT_EQUAL(wtc_init_packet(p_st_wtc_packet), FR_SUCC);

    p_st_wtc_packet->fragment_state = APM_WTC_FRAGMENT_NEED_NEXT_REQ_PACKET;
    p_st_wtc_packet->ui_len_to_read = 1500;

    CU_ASSERT_EQUAL(wtc_packet_unfragment(p_st_wtc_sess, p_st_packet, p_st_wtc_packet), APM_WTC_CHECK_FRAGMENT_NEED_NEXT_PACKET);
    CU_ASSERT_EQUAL(p_st_wtc_packet->st_timestamp_last.tv_sec, 1454549354L);

    __wtc_destroy_packet(&p_st_wtc_packet);
    CU_ASSERT_PTR_NULL(p_st_wtc_packet);

    apmtest_destroy_apm_packet(&p_st_packet);
    CU_ASSERT_PTR_NULL(p_st_packet);

    __wtc_destroy_session(&p_st_wtc_sess);
    CU_ASSERT_PTR_NULL(p_st_wtc_sess);
}

static void __case5(void)
{
    L7session_wtc *p_st_wtc_sess = NULL;
    p_st_wtc_sess = (L7session_wtc *) frmalloc(sizeof(L7session_wtc));
    CU_ASSERT_PTR_NOT_NULL(p_st_wtc_sess);
    memset(p_st_wtc_sess, 0x00, sizeof(L7session_wtc));

    char *p_bin = (char *) "739038420000000e00000001ffffffff0000000000000000000000010000021c0000005700000054000000430000004c0000006f00000063000000610000006c00000041000000500000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000480000001400000001000100";
    char arr_payload[65536] = {'\0'};

    CU_ASSERT_EQUAL(apmtest_hex_string_to_bin(p_bin, arr_payload, sizeof(arr_payload)), FR_SUCC);

    unsigned int ui_payload_len = strlen(p_bin) / 2;
    Apm_packet *p_st_packet = NULL;
    struct timeval st_timestamp;
    st_timestamp.tv_sec = 1454549354L;
    st_timestamp.tv_usec = 123456L;
    CU_ASSERT_EQUAL(apmtest_create_apm_packet(&p_st_packet, arr_payload, ui_payload_len, TEST_WTC_SRC_IP, TEST_WTC_DST_IP, TEST_WTC_SRC_PORT, TEST_WTC_DST_PORT, st_timestamp), FR_SUCC);
    p_st_packet->i_direction = FR_DIRECT_CLIENT2SRV;

    L7_wtc_packet *p_st_wtc_packet = NULL;
    p_st_wtc_packet = (L7_wtc_packet *) frmalloc(sizeof(L7_wtc_packet));
    CU_ASSERT_PTR_NOT_NULL(p_st_wtc_packet);
    memset(p_st_wtc_packet, 0x00, sizeof(L7_wtc_packet));

    CU_ASSERT_EQUAL(wtc_init_packet(p_st_wtc_packet), FR_SUCC);

    p_st_wtc_packet->fragment_state = APM_WTC_FRAGMENT_NEED_NEXT_RESP_PACKET;

    CU_ASSERT_EQUAL(wtc_packet_unfragment(p_st_wtc_sess, p_st_packet, p_st_wtc_packet), APM_WTC_CHECK_FRAGMENT_NEED_NEXT_PACKET);
    CU_ASSERT_EQUAL(p_st_wtc_packet->st_timestamp_last.tv_sec, 0L);

    __wtc_destroy_packet(&p_st_wtc_packet);
    CU_ASSERT_PTR_NULL(p_st_wtc_packet);

    apmtest_destroy_apm_packet(&p_st_packet);
    CU_ASSERT_PTR_NULL(p_st_packet);

    __wtc_destroy_session(&p_st_wtc_sess);
    CU_ASSERT_PTR_NULL(p_st_wtc_sess);
}

int add_suite_wtc_packet_unfragment(void)
{
    CU_pSuite pSuite = NULL;

    if ((pSuite = CU_add_suite(MODULE_NAME" wtc_packet_unfragment()", __suite_init, __suite_cleanup)) == NULL)
        return FR_FAIL;

    if (CU_add_test(pSuite, "case1", __case1) == NULL ||
        CU_add_test(pSuite, "case2", __case2) == NULL ||
        CU_add_test(pSuite, "case3", __case3) == NULL ||
        CU_add_test(pSuite, "case4", __case4) == NULL ||
        CU_add_test(pSuite, "case5", __case5) == NULL)
        return FR_FAIL;

    return FR_SUCC;
}
